package cn.com.css.taxInspection.attention.service.impl;

import cn.com.css.taxInspection.attention.entity.AjglJctzsqcEntity;
import cn.com.css.taxInspection.attention.mapper.AjglJctzsqcMapper;
import cn.com.css.taxInspection.attention.service.AjglJctzsqcService;
import cn.com.css.taxInspection.gt3.service.DmGySwjgService;
import cn.com.css.taxInspection.gt3.service.DmGySwryService;
import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.util.TsBqUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.StringJoiner;

@Service
@Slf4j
public class AjglJctzsqcServiceImpl extends ServiceImpl<AjglJctzsqcMapper, AjglJctzsqcEntity> implements AjglJctzsqcService {


    @Resource
    private DmGySwjgService dmGySwjgService;
    @Resource
    private DmGySwryService dmGySwryService;
    @Resource
    private HttpSessionService httpSessionService;


    @Override
    public IPage<AjglJctzsqcEntity> queryJctzsTable(AjglJctzsqcEntity ajglJctzsqcEntity) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        IPage<AjglJctzsqcEntity> page = new Page<>(ajglJctzsqcEntity.getPage(), ajglJctzsqcEntity.getLimit());
        String swjg = httpSessionService.getLogSwjgDm();
        LambdaQueryWrapper<AjglJctzsqcEntity> queryWrapper = Wrappers.lambdaQuery();
        if (TsBqUtils.isNotNull(swjg)) {
            queryWrapper.eq(AjglJctzsqcEntity::getSwjgDm, swjg);
        }
        //案件编号
        if (TsBqUtils.isNotNull(ajglJctzsqcEntity.getJcajbh())) {
            queryWrapper.like(AjglJctzsqcEntity::getJcajbh, ajglJctzsqcEntity.getJcajbh());
        }
        //纳税人识别号
        if (TsBqUtils.isNotNull(ajglJctzsqcEntity.getNsrsbh())) {
            queryWrapper.like(AjglJctzsqcEntity::getNsrsbh, ajglJctzsqcEntity.getNsrsbh());
        }
        //纳税人名称
        if (TsBqUtils.isNotNull(ajglJctzsqcEntity.getNsrmc())) {
            queryWrapper.like(AjglJctzsqcEntity::getNsrmc, ajglJctzsqcEntity.getNsrmc());
        }
        //是否有效
        if (TsBqUtils.isNotNull(ajglJctzsqcEntity.getSdztDm())) {
            queryWrapper.like(AjglJctzsqcEntity::getSdztDm, ajglJctzsqcEntity.getSdztDm());
        }
        //日期
        if (TsBqUtils.isNotNull(ajglJctzsqcEntity.getStartTime())) {
            queryWrapper.between(AjglJctzsqcEntity::getJcrqq, sdf.parse(ajglJctzsqcEntity.getStartTime()), sdf.parse(ajglJctzsqcEntity.getEndTime()));
        }
        //销号日期
        if (TsBqUtils.isNotNull(ajglJctzsqcEntity.getXhStartTime())) {
            queryWrapper.between(AjglJctzsqcEntity::getXhrq, sdf.parse(ajglJctzsqcEntity.getXhStartTime()), sdf.parse(ajglJctzsqcEntity.getXhEndTime()));
        }
        queryWrapper.orderByDesc(AjglJctzsqcEntity::getXhrq);
        IPage<AjglJctzsqcEntity> resPage = page(page, queryWrapper);
        List<AjglJctzsqcEntity> ajglJctzsqcEntityList = resPage.getRecords();
        for (AjglJctzsqcEntity ajglJctzsqc : ajglJctzsqcEntityList) {
            ajglJctzsqc.setLrrDm(ajglJctzsqc.getLrrDm() + "|" + dmGySwryService.getSwry(ajglJctzsqc.getLrrDm()).getSwryxm());
            ajglJctzsqc.setSwjgDm(dmGySwjgService.getSwjgDmBySwjgJc(ajglJctzsqc.getSwjgDm()));
            String[] parts = ajglJctzsqc.getJcry().split(",");
            List<String> list = Arrays.asList(parts);
            StringJoiner joiner = new StringJoiner(",");
            list.forEach(part -> joiner.add(part + "|" + dmGySwryService.getSwry(part).getSwryxm()));
            ajglJctzsqc.setJcry(joiner.toString());
        }
        return resPage;
    }

    @Override
    public List<AjglJctzsqcEntity> exportJctzsTable(AjglJctzsqcEntity ajglJctzsqcEntity) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String swjg = httpSessionService.getLogSwjgDm();
        LambdaQueryWrapper<AjglJctzsqcEntity> queryWrapper = Wrappers.lambdaQuery();
        if (TsBqUtils.isNotNull(swjg)) {
            queryWrapper.eq(AjglJctzsqcEntity::getSwjgDm, swjg);
        }        //案件编号
        if (TsBqUtils.isNotNull(ajglJctzsqcEntity.getJcajbh())) {
            queryWrapper.like(AjglJctzsqcEntity::getJcajbh, ajglJctzsqcEntity.getJcajbh());
        }
        //纳税人识别号
        if (TsBqUtils.isNotNull(ajglJctzsqcEntity.getNsrsbh())) {
            queryWrapper.like(AjglJctzsqcEntity::getNsrsbh, ajglJctzsqcEntity.getNsrsbh());
        }
        //纳税人名称
        if (TsBqUtils.isNotNull(ajglJctzsqcEntity.getNsrmc())) {
            queryWrapper.like(AjglJctzsqcEntity::getNsrmc, ajglJctzsqcEntity.getNsrmc());
        }
        //是否有效
        if (TsBqUtils.isNotNull(ajglJctzsqcEntity.getSdztDm())) {
            queryWrapper.like(AjglJctzsqcEntity::getSdztDm, ajglJctzsqcEntity.getSdztDm());
        }
        //日期
        if (TsBqUtils.isNotNull(ajglJctzsqcEntity.getStartTime())) {
            queryWrapper.between(AjglJctzsqcEntity::getJcrqq, sdf.parse(ajglJctzsqcEntity.getStartTime()), sdf.parse(ajglJctzsqcEntity.getEndTime()));
        }
        //销号日期
        if (TsBqUtils.isNotNull(ajglJctzsqcEntity.getXhStartTime())) {
            queryWrapper.between(AjglJctzsqcEntity::getXhrq, sdf.parse(ajglJctzsqcEntity.getXhStartTime()), sdf.parse(ajglJctzsqcEntity.getXhEndTime()));
        }
        queryWrapper.orderByDesc(AjglJctzsqcEntity::getXhrq);
        List<AjglJctzsqcEntity> ajglJctzsqcEntityList = this.list(queryWrapper);
        for (AjglJctzsqcEntity ajglJctzsqc : ajglJctzsqcEntityList) {
            ajglJctzsqc.setLrrDm(ajglJctzsqc.getLrrDm() + "|" + dmGySwryService.getSwry(ajglJctzsqc.getLrrDm()).getSwryxm());
            ajglJctzsqc.setSwjgDm(dmGySwjgService.getSwjgDmBySwjgJc(ajglJctzsqc.getSwjgDm()));
            String[] parts = ajglJctzsqc.getJcry().split(",");
            List<String> list = Arrays.asList(parts);
            StringJoiner joiner = new StringJoiner(",");
            list.forEach(part -> joiner.add(part + "|" + dmGySwryService.getSwry(part).getSwryxm()));
            ajglJctzsqc.setJcry(joiner.toString());
        }
        return ajglJctzsqcEntityList;
    }
}
